package com.wyx.config.shiro;

import com.wyx.constans.Constans;
import org.apache.shiro.util.StringUtils;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
import org.apache.shiro.web.util.WebUtils;
import org.springframework.context.annotation.Configuration;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.io.Serializable;
import java.util.UUID;

/**
 * @author xiaoxiong
 * @version 1.0
 * @date 2021/2/24 22:24
 * 生成token
 * 如果没有就生成一个token 返回给前端
 * 如果有token就从请求头里面取一个出来
 */
@Configuration
public class TokenWebSessionManager extends DefaultWebSessionManager {

    /**
     *
     * @param request
     * @param response
     * @return
     */
    @Override
    protected Serializable getSessionId(ServletRequest request, ServletResponse response) {
        String token = WebUtils.toHttp(request).getHeader(Constans.TOKEN);
        if(StringUtils.hasText(token)){
            return token;
        }else {
            return UUID.randomUUID().toString();
        }
    }

}
